package _34define.homework;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Demo {
    public static void main(String[] args) {
        testDistinct();
        cityDistinct();
        nameDistinct();
        name2Distinct();
        System.out.println(beijingDistinct());
        beijngMoneyDistinct();
        minMoneyDistint();
        minMoneyEx();
    }

    private static void minMoneyEx() {
        List<Transaction> transactions=TransactionRecord.transactions;
        List<Transaction> list=transactions.stream().sorted(((o1, o2) -> {
                    return o1.getValue() - o2.getValue();
                }))
                .collect(Collectors.toList());
        for (Transaction transaction : list) {
            System.out.println(transaction);
            break;
        }
    }

    private static void minMoneyDistint() {
        List<Transaction> transactions=TransactionRecord.transactions;
        List<Integer> list=new ArrayList<>();
        for (Transaction transaction : transactions) {
            list.add(transaction.getValue());
        }
        list.stream().sorted();
        for (Integer i : list) {
            System.out.println(i);
            break;
        }
    }

    private static void beijngMoneyDistinct() {
        List<Transaction> transactions=TransactionRecord.transactions;
        for (Transaction transaction : transactions) {
            if (transaction.getTrader().getCity().equals("beijing")){
                System.out.println(transaction);
            }
        }
    }

    private static boolean beijingDistinct() {
        List<Transaction> transactions=TransactionRecord.transactions;
        for (Transaction transaction : transactions) {
            if (transaction.getTrader().getCity().equals("beijing")){
                return true;
            }
        }
        return false;
    }

    private static void name2Distinct() {
        List<Transaction> transactions=TransactionRecord.transactions;
        List<String> arr=new ArrayList<>();
        for (Transaction transaction : transactions) {
            arr.add(transaction.getTrader().getName());
        }
        arr.stream().sorted();
        System.out.println(arr);
    }

    private static void nameDistinct() {
        List<Transaction> transactions=TransactionRecord.transactions;
        List<Transaction> list=transactions.stream().filter(new Predicate<Transaction>() {
                    @Override
                    public boolean test(Transaction transaction) {
                        return transaction.getTrader().getCity().equals("beijing");
                    }
                }).sorted(((o1, o2) -> {
                    return o1.getTrader().getName().hashCode() - o2.getTrader().getName().hashCode();
                }))
                .collect(Collectors.toList());
        System.out.println(list);
        System.out.println(list.size());
    }

    private static void cityDistinct() {
        List<Transaction> transactions=TransactionRecord.transactions;
        Map<String,Integer> map=new HashMap<>();
        for (Transaction transaction : transactions) {
           String a= transaction.getTrader().getCity();
           map.put(a,map.getOrDefault(a,0)+1);
        }
        System.out.println(map.keySet());
        System.out.println(map.size());
    }

    private static void testDistinct() {
        List<Transaction> transactions=TransactionRecord.transactions;
        List<Transaction> list=transactions.stream().filter(new Predicate<Transaction>() {
            @Override
            public boolean test(Transaction transaction) {
                return transaction.getYear()==2011;
            }
        }).sorted(((o1, o2) -> o1.getValue()- o2.getValue()))
                .collect(Collectors.toList());
        System.out.println(list);
        System.out.println(list.size());
    }
}
